<?php

namespace app\index\controller;

use app\index\model\AccountListModel;
use app\index\model\BidBookModel;
use app\index\model\BidCustomerModel;
use app\index\model\BidGrantBookModel;
use app\index\model\BidModel;
use app\index\model\BidPoolModel;
use app\index\model\BidSumListModel;
use app\index\model\BidSumModel;
use app\index\model\BidTaskGeneralModel;
use app\index\model\BidTaskModel;
use app\index\model\BidTaskTemplateModel;
use app\index\model\CertUseBidModel;
use app\index\model\CertUseModel;
use app\index\model\DealcomModel;
use app\index\model\DeptModel;
use app\index\model\EnginBondFanModel;
use app\index\model\EnginBondJiaoModel;
use app\index\model\EnginBondKaiModel;
use app\index\model\EnginBondShouModel;
use app\index\model\FileModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\UserModel;
use app\index\relevan\CertRelevan;
use app\index\service\BidService;
use app\index\service\FlowService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\UserService;
use app\index\service\WorkerService;
use app\index\validate\BidSum;
use app\index\validate\CertUse;
use think\Db;
use think\Request;

class Bid extends Common
{
    public function list_bid_pool(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['bid.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bid.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['bidtype']) && !empty($param['bidtype'])){
            $where[] = ['bid.bidtype','=', $param['bidtype']];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['bid.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['zhaoname']) && !empty($param['zhaoname'])){
            $where[] = ['bid.zhaoname','like', '%'.$param['zhaoname'].'%'];
        }
        if(isset($param['zhaotel']) && !empty($param['zhaotel'])){
            $where[] = ['bid.zhaotel','like', '%'.$param['zhaotel'].'%'];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['bid.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['bid.business','=', $param['business']];
        }
        if(isset($param['jingbeier']) && !empty($param['jingbeier'])){
            $where[] = ['bid.jingbeier','like', '%'.$param['jingbeier'].'%'];
        }
        if(isset($param['xinlai']) && !empty($param['xinlai'])){
            $where[] = ['bid.xinlai','like', '%'.$param['xinlai'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['bid.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['bidtype'] = (new OptService())->get_base_info('engin_engintype');
        $data['is_bidtype'] = isset($param['bidtype']) ? $param['bidtype'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';


        $data['data'] = BidPoolModel::alias('bid')
            ->field('bid.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid_pool', 'bid'))
            ->order('bid.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['bidtype_name'] = (new OptService())->get_base_showname('engin_engintype', $item['bidtype']);
                $item['zhaotype_name'] = (new OptService())->get_base_showname('bid_bidding_type', $item['zhaotype']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('bid_pool');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_bid_pool(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['name'])){
                return outputJson(4000, '请输入项目名称');
            }
            if((new BidService())->check_bid_name($param['name'])){
                return outputJson(4000, '项目名称已存在');
            }
            if(empty($param['chanid'])){
                return outputJson(4000, '请选择所属单位');
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = BidPoolModel::insertGetId($param);

                (new OptService())->relation_file($files,'bid_pool',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'bid_pool', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'bid_pool', $id, '投标信息池');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['bidtype'] = (new OptService())->get_base_info('engin_engintype');
            $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
            $data['areas'] = (new OptService())->get_city_cader();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_bid_pool(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if(empty($param['name'])){
                return outputJson(4000, '请输入项目名称');
            }
            if((new BidService())->check_bid_name($param['name'], $param['id'])){
                return outputJson(4000, '项目名称已存在');
            }
            if(empty($param['chanid'])){
                return outputJson(4000, '请选择所属单位');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                BidPoolModel::update($param);


                (new OptService())->relation_file($files,'bid_pool',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'bid_pool', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'bid_pool', $param['id'], '投标信息池');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = BidPoolModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['data']['chanid_info'] = (new UserService())->get_users_part_arr($data['data']['chanid'], 'dept');
            $data['bidtype'] = (new OptService())->get_base_info('engin_engintype');
            $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
            $data['areas'] = (new OptService())->get_city_cader();

            $data['files'] = (new OptService())->get_file('bid_pool', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_bid_pool(Request $request){
        $param = $request->post();
        $data = BidPoolModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            BidPoolModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'bid_pool', $data['id'], '投标信息池');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_bid_pool(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = BidPoolModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['data']['chanid_name'] = DeptModel::get_name_by_pk($data['data']['chanid']);
        $data['data']['bidtype_name'] = (new OptService())->get_base_showname('engin_engintype', $data['data']['bidtype']);
        $data['data']['zhaotype_name'] = (new OptService())->get_base_showname('bid_bidding_type', $data['data']['zhaotype']);
        $data['data']['business_name'] = $data['data']['business'] == 1 ? '自营' : '联营';
        $data['files'] = (new OptService())->get_file('bid_pool', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }



    public function list_bid(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];
        $where[] = ['bid.isturn','=',1];
        $where[] = ['bid.status','in',[0,1]];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['bid.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bid.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['bidtype']) && !empty($param['bidtype'])){
            $where[] = ['bid.bidtype','=', $param['bidtype']];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['bid.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['zhaoname']) && !empty($param['zhaoname'])){
            $where[] = ['bid.zhaoname','like', '%'.$param['zhaoname'].'%'];
        }
        if(isset($param['zhaotel']) && !empty($param['zhaotel'])){
            $where[] = ['bid.zhaotel','like', '%'.$param['zhaotel'].'%'];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['bid.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['bid.business','=', $param['business']];
        }
        if(isset($param['jingbeier']) && !empty($param['jingbeier'])){
            $where[] = ['bid.jingbeier','like', '%'.$param['jingbeier'].'%'];
        }
        if(isset($param['xinlai']) && !empty($param['xinlai'])){
            $where[] = ['bid.xinlai','like', '%'.$param['xinlai'].'%'];
        }
        if(isset($param['enddt_bao']) && !empty($param['enddt_bao'])){
            $where[] = ['bid.enddt_bao','between', explode(' - ', $param['enddt_bao'])];
        }
        if(isset($param['enddt_tou']) && !empty($param['enddt_tou'])){
            $where[] = ['bid.enddt_tou','between', explode(' - ', $param['enddt_tou'])];
        }
        if(isset($param['kaidt']) && !empty($param['kaidt'])){
            $where[] = ['bid.kaidt','between', explode(' - ', $param['kaidt'])];
        }
        if(isset($param['touer_name']) && !empty($param['touer_name'])){
            $where[] = ['tou.realname','like', '%'.$param['touer_name'].'%'];
        }
        if(isset($param['touunit']) && !empty($param['touunit'])){
            $where[] = ['bid.touunit','=', $param['touunit']];
        }
        if(isset($param['toushuo']) && !empty($param['toushuo'])){
            $where[] = ['bid.toushuo','like', '%'.$param['toushuo'].'%'];
        }
        if(isset($param['lianunit']) && !empty($param['lianunit'])){
            $where[] = ['bid.lianunit','like', '%'.$param['lianunit'].'%'];
        }
        if(isset($param['isfei']) && !empty($param['isfei'])){
            $where[] = ['bid.isfei','=', $param['isfei']];
        }
        if(isset($param['feishuo']) && !empty($param['feishuo'])){
            $where[] = ['bid.feishuo','like', '%'.$param['feishuo'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['bid.state','=', $param['state']];
        }



        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['bid.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['touunit'] = $data['chanid'];
        $data['is_touunit'] = isset($param['touunit']) ? $param['touunit'] : '';

        $data['bidtype'] = (new OptService())->get_base_info('engin_engintype');
        $data['is_bidtype'] = isset($param['bidtype']) ? $param['bidtype'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';

        $data['state'] = (new BidService())->get_bid_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['data'] = BidModel::alias('bid')
            ->field('bid.*,tou.realname as touer_name')
            ->leftJoin('user tou','tou.id=bid.touer')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid', 'bid'))
            ->order('bid.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['bidtype_name'] = (new OptService())->get_base_showname('engin_engintype', $item['bidtype']);
                $item['zhaotype_name'] = (new OptService())->get_base_showname('bid_bidding_type', $item['zhaotype']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['state_name'] = (new BidService())->get_bid_state_name_tag($item);
                $item['state_name_wen'] = (new BidService())->get_bid_state_name_wen($item);
                $item['touunit_name'] = DeptModel::where(['id'=>$item['touunit']])->value('name');
                $item['bianer_name'] = UserModel::get_realname_by_ids($item['bianer']);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('bid');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function edit_bid(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['name'])){
                return outputJson(4000, '请输入项目名称');
            }
            if((new BidService())->check_bid_name($param['name'], $param['id'])){
                return outputJson(4000, '项目名称已存在');
            }
            if(empty($param['chanid'])){
                return outputJson(4000, '请选择所属单位');
            }
            if(empty($param['enddt_tou'])){
                return outputJson(4000, '请选择投标截止时间');
            }
            if(empty($param['touer'])){
                return outputJson(4000, '请选择投标负责人');
            }
            if(empty($param['touunit'])){
                return outputJson(4000, '请选择投标单位');
            }
            if(empty($param['business'])){
                return outputJson(4000, '请选择项目属性');
            }
            if($param['business'] == 2){
                if(empty($param['lianunit'])){
                    return outputJson(4000, '请选择联营单位');
                }
                if(empty($param['isfei'])){
                    return outputJson(4000, '请选择是否收费');
                }
            }else{
                $param['lianunit'] = '';
                $param['isfei'] = '';
                $param['feishuo'] = '';
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                BidModel::update($param);

                (new OptService())->relation_file($files,'bid',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'bid', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'bid', $param['id'], '投标项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = BidModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['chanid_info'] = (new UserService())->get_users_part_arr($data['data']['chanid'], 'dept');
            $data['data']['touunit_info'] = (new UserService())->get_users_part_arr($data['data']['touunit'], 'dept');
            $data['data']['touer_info'] = (new UserService())->get_users_part_arr($data['data']['touer'], 'user');


            $data['bidtype'] = (new OptService())->get_base_info('engin_engintype');
            $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
            $data['areas'] = (new OptService())->get_city_cader();


            $data['files'] = (new OptService())->get_file('bid', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_bid(Request $request){
        $param = $request->post();
        $data = BidModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            BidModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'bid', $data['id'], '投标项目');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_bid(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = BidModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['data']['chanid_name'] = DeptModel::get_name_by_pk($data['data']['chanid']);
        $data['data']['bidtype_name'] = (new OptService())->get_base_showname('engin_engintype', $data['data']['bidtype']);
        $data['data']['zhaotype_name'] = (new OptService())->get_base_showname('bid_bidding_type', $data['data']['zhaotype']);
        $data['data']['business_name'] = $data['data']['business'] == 1 ? '自营' : '联营';
        $data['data']['state_name'] = (new BidService())->get_bid_state_name_tag($data['data']);
        $data['data']['state_name_wen'] = (new BidService())->get_bid_state_name_wen($data['data']);
        $data['data']['touunit_name'] = DeptModel::where(['id'=>$data['data']['touunit']])->value('name');
        $data['data']['touer_name'] = UserModel::get_realname_by_pk($data['data']['touer']);
        $data['data']['tou_kaier_name'] = UserModel::get_realname_by_pk($data['data']['tou_kaier']);
        $data['data']['bianer_name'] = UserModel::get_realname_by_ids($data['data']['bianer']);

        $data['data']['state_jie'] = '';
        if($data['data']['state'] == 3){
            $data['data']['state_jie'] = '中标';
        }
        if($data['data']['state'] == 6){
            $data['data']['state_jie'] = '未中标';
        }


        $data['files'] = (new OptService())->get_file('bid', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function bao_bid(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                BidModel::update($param);

                (new OptService())->relation_file($files,'bid',$param['id']);
                (new OptService())->add_flow_log('报名', 'bid', $param['id']);
                (new OptService())->add_log_list('报名', 'bid', $param['id'], '投标项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = BidModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['files'] = (new OptService())->get_file('bid', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function tou_bid(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                BidModel::update($param);

                (new OptService())->relation_file($files,'bid',$param['id']);
                (new OptService())->add_flow_log('投标', 'bid', $param['id']);
                (new OptService())->add_log_list('投标', 'bid', $param['id'], '投标项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = BidModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['tou_kaier_info'] = (new UserService())->get_users_part_arr($data['data']['tou_kaier'], 'user');
            $data['tou_kaitype'] = (new BidService())->get_bid_tou_kaitype();
            $data['files'] = (new OptService())->get_file('bid', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function kai_bid(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            if(!$param['state']){
                $param['state'] = 1;
            }

            if($param['state'] != 3){
                $param['kai_windt'] = '';
                $param['kai_fee'] = '';
                $param['kai_num'] = '';
            }


            $oldinfo = BidModel::where(['id'=>$param['id']])->find();

            Db::startTrans();
            try {
                BidModel::update($param);

                if($oldinfo['isqi'] == 0){
                    if($oldinfo['state'] == 1 || $oldinfo['state'] == 3){
                        if($param['state'] == 6){
                            (new CertRelevan())->set_cert_statueuse_bid($param['id']);
                        }
                    }else{
                        if($param['state'] == 1 || $param['state'] == 3){
                            (new CertRelevan())->set_cert_statueuse_bid($param['id']);
                        }
                    }
                }



                (new OptService())->relation_file($files,'bid',$param['id']);
                (new OptService())->add_flow_log('开标', 'bid', $param['id']);
                (new OptService())->add_log_list('开标', 'bid', $param['id'], '投标项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = BidModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['kai_guo'] = (new BidService())->get_bid_kai_guo();
            $data['files'] = (new OptService())->get_file('bid', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function bian_bid(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            unset($param['token']);

            Db::startTrans();
            try {
                BidModel::update($param);

                (new OptService())->add_flow_log('设置编标员', 'bid', $param['id']);
                (new OptService())->add_log_list('设置编标员', 'bid', $param['id'], '投标项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = BidModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['bianer_info'] = (new UserService())->get_users_part_arr($data['data']['bianer'], 'user');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function qi_bid(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            unset($param['token']);

            $param['isqi'] = 1;

            Db::startTrans();
            try {
                BidModel::update($param);

                (new CertRelevan())->set_cert_statueuse_bid($param['id']);

                (new OptService())->add_flow_log('放弃', 'bid', $param['id']);
                (new OptService())->add_log_list('放弃', 'bid', $param['id'], '投标项目');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }
        return outputJson(4000, lang('code_4000'));
    }

    public function list_bid_jindu(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];
        $where[] = ['bid.isturn','=',1];
        $where[] = ['bid.status','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['bid.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bid.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['bidtype']) && !empty($param['bidtype'])){
            $where[] = ['bid.bidtype','=', $param['bidtype']];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['bid.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['zhaoname']) && !empty($param['zhaoname'])){
            $where[] = ['bid.zhaoname','like', '%'.$param['zhaoname'].'%'];
        }
        if(isset($param['zhaotel']) && !empty($param['zhaotel'])){
            $where[] = ['bid.zhaotel','like', '%'.$param['zhaotel'].'%'];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['bid.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['bid.business','=', $param['business']];
        }
        if(isset($param['jingbeier']) && !empty($param['jingbeier'])){
            $where[] = ['bid.jingbeier','like', '%'.$param['jingbeier'].'%'];
        }
        if(isset($param['xinlai']) && !empty($param['xinlai'])){
            $where[] = ['bid.xinlai','like', '%'.$param['xinlai'].'%'];
        }
        if(isset($param['enddt_bao']) && !empty($param['enddt_bao'])){
            $where[] = ['bid.enddt_bao','between', explode(' - ', $param['enddt_bao'])];
        }
        if(isset($param['enddt_tou']) && !empty($param['enddt_tou'])){
            $where[] = ['bid.enddt_tou','between', explode(' - ', $param['enddt_tou'])];
        }
        if(isset($param['kaidt']) && !empty($param['kaidt'])){
            $where[] = ['bid.kaidt','between', explode(' - ', $param['kaidt'])];
        }
        if(isset($param['touer_name']) && !empty($param['touer_name'])){
            $where[] = ['tou.realname','like', '%'.$param['touer_name'].'%'];
        }
        if(isset($param['touunit']) && !empty($param['touunit'])){
            $where[] = ['bid.touunit','=', $param['touunit']];
        }
        if(isset($param['toushuo']) && !empty($param['toushuo'])){
            $where[] = ['bid.toushuo','like', '%'.$param['toushuo'].'%'];
        }
        if(isset($param['lianunit']) && !empty($param['lianunit'])){
            $where[] = ['bid.lianunit','like', '%'.$param['lianunit'].'%'];
        }
        if(isset($param['isfei']) && !empty($param['isfei'])){
            $where[] = ['bid.isfei','=', $param['isfei']];
        }
        if(isset($param['feishuo']) && !empty($param['feishuo'])){
            $where[] = ['bid.feishuo','like', '%'.$param['feishuo'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['bid.state','=', $param['state']];
        }



        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['bid.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['touunit'] = $data['chanid'];
        $data['is_touunit'] = isset($param['touunit']) ? $param['touunit'] : '';

        $data['bidtype'] = (new OptService())->get_base_info('engin_engintype');
        $data['is_bidtype'] = isset($param['bidtype']) ? $param['bidtype'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';

        $data['state'] = (new BidService())->get_bid_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['data'] = BidModel::alias('bid')
            ->field('bid.*,tou.realname as touer_name')
            ->leftJoin('user tou','tou.id=bid.touer')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid_jindu', 'bid'))
            ->order('bid.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['state_name'] = (new BidService())->get_bid_state_name_tag($item);
                $item['state_name_wen'] = (new BidService())->get_bid_state_name_wen($item);
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['touunit_name'] = DeptModel::where(['id'=>$item['touunit']])->value('name');
                $item['bianer_name'] = UserModel::get_realname_by_ids($item['bianer']);

                $item['is_bao'] = false;
                if(!empty($item['bao_dt']) && $item['bao_dt'] != '0000-00-00 00:00:00'){
                    $item['is_bao'] = true;
                }

                $item['is_book'] = false;
                $exist_book = BidBookModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1])
                    ->where(['bidid'=>$item['id']])->where('status','in',[0,1])->find();
                if($exist_book){
                    $item['is_book'] = true;
                }

                $item['is_use'] = false;
                $exist_use = CertUseBidModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1])
                    ->where(['bidid'=>$item['id']])->where('status','in',[0,1])->find();
                if($exist_use){
                    $item['is_use'] = true;
                }

                $item['is_baojin'] = false;
                $exist_baojiao = EnginBondJiaoModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1])
                    ->where(['bidid'=>$item['id']])->where('status','in',[0,1])->find();
                if($exist_baojiao){
                    $item['is_baojin'] = true;
                }
                $exist_baokai = EnginBondKaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1])
                    ->where(['bidid'=>$item['id']])->where('status','in',[0,1])->find();
                if($exist_baokai){
                    $item['is_baojin'] = true;
                }

                $item['is_tou'] = false;
                if(!empty($item['tou_dt']) && $item['tou_dt'] != '0000-00-00 00:00:00'){
                    $item['is_tou'] = true;
                }

                $item['is_kai'] = false;
                if($item['state'] == 3 || $item['state'] == 6){
                    $item['is_kai'] = true;
                }

                $item['is_zong'] = false;
                $exist_zong = BidSumModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])->where(['bidid'=>$item['id']])->find();
                if($exist_zong){
                    $item['is_zong'] = true;
                }

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_bid_win(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];
        $where[] = ['bid.isturn','=',1];
        $where[] = ['bid.status','=',1];
        $where[] = ['bid.state','=',3];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['bid.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bid.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['bidtype']) && !empty($param['bidtype'])){
            $where[] = ['bid.bidtype','=', $param['bidtype']];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['bid.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['zhaoname']) && !empty($param['zhaoname'])){
            $where[] = ['bid.zhaoname','like', '%'.$param['zhaoname'].'%'];
        }
        if(isset($param['zhaotel']) && !empty($param['zhaotel'])){
            $where[] = ['bid.zhaotel','like', '%'.$param['zhaotel'].'%'];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['bid.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['bid.business','=', $param['business']];
        }
        if(isset($param['jingbeier']) && !empty($param['jingbeier'])){
            $where[] = ['bid.jingbeier','like', '%'.$param['jingbeier'].'%'];
        }
        if(isset($param['xinlai']) && !empty($param['xinlai'])){
            $where[] = ['bid.xinlai','like', '%'.$param['xinlai'].'%'];
        }
        if(isset($param['enddt_bao']) && !empty($param['enddt_bao'])){
            $where[] = ['bid.enddt_bao','between', explode(' - ', $param['enddt_bao'])];
        }
        if(isset($param['enddt_tou']) && !empty($param['enddt_tou'])){
            $where[] = ['bid.enddt_tou','between', explode(' - ', $param['enddt_tou'])];
        }
        if(isset($param['kaidt']) && !empty($param['kaidt'])){
            $where[] = ['bid.kaidt','between', explode(' - ', $param['kaidt'])];
        }
        if(isset($param['touer_name']) && !empty($param['touer_name'])){
            $where[] = ['tou.realname','like', '%'.$param['touer_name'].'%'];
        }
        if(isset($param['touunit']) && !empty($param['touunit'])){
            $where[] = ['bid.touunit','=', $param['touunit']];
        }
        if(isset($param['toushuo']) && !empty($param['toushuo'])){
            $where[] = ['bid.toushuo','like', '%'.$param['toushuo'].'%'];
        }
        if(isset($param['lianunit']) && !empty($param['lianunit'])){
            $where[] = ['bid.lianunit','like', '%'.$param['lianunit'].'%'];
        }
        if(isset($param['isfei']) && !empty($param['isfei'])){
            $where[] = ['bid.isfei','=', $param['isfei']];
        }
        if(isset($param['feishuo']) && !empty($param['feishuo'])){
            $where[] = ['bid.feishuo','like', '%'.$param['feishuo'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['bid.state','=', $param['state']];
        }



        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['bid.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['touunit'] = $data['chanid'];
        $data['is_touunit'] = isset($param['touunit']) ? $param['touunit'] : '';

        $data['bidtype'] = (new OptService())->get_base_info('engin_engintype');
        $data['is_bidtype'] = isset($param['bidtype']) ? $param['bidtype'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';

        $data['state'] = (new BidService())->get_bid_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['data'] = BidModel::alias('bid')
            ->field('bid.*,tou.realname as touer_name')
            ->leftJoin('user tou','tou.id=bid.touer')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid_win', 'bid'))
            ->order('bid.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['bidtype_name'] = (new OptService())->get_base_showname('engin_engintype', $item['bidtype']);
                $item['zhaotype_name'] = (new OptService())->get_base_showname('bid_bidding_type', $item['zhaotype']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['state_name'] = (new BidService())->get_bid_state_name_tag($item);
                $item['state_name_wen'] = (new BidService())->get_bid_state_name_wen($item);
                $item['touunit_name'] = DeptModel::where(['id'=>$item['touunit']])->value('name');
                $item['bianer_name'] = UserModel::get_realname_by_ids($item['bianer']);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_bidmy(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];
        $where[] = ['bid.isturn','=',1];
        $where[] = ['bid.status','in',[0,1]];


        $whereMy = "bid.created_user=".$this->userinfo['id']." or bid.touer=".$this->userinfo['id'];





        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['bid.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bid.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['bidtype']) && !empty($param['bidtype'])){
            $where[] = ['bid.bidtype','=', $param['bidtype']];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['bid.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['zhaoname']) && !empty($param['zhaoname'])){
            $where[] = ['bid.zhaoname','like', '%'.$param['zhaoname'].'%'];
        }
        if(isset($param['zhaotel']) && !empty($param['zhaotel'])){
            $where[] = ['bid.zhaotel','like', '%'.$param['zhaotel'].'%'];
        }
        if(isset($param['zhaotype']) && !empty($param['zhaotype'])){
            $where[] = ['bid.zhaotype','=', $param['zhaotype']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['bid.business','=', $param['business']];
        }
        if(isset($param['jingbeier']) && !empty($param['jingbeier'])){
            $where[] = ['bid.jingbeier','like', '%'.$param['jingbeier'].'%'];
        }
        if(isset($param['xinlai']) && !empty($param['xinlai'])){
            $where[] = ['bid.xinlai','like', '%'.$param['xinlai'].'%'];
        }
        if(isset($param['enddt_bao']) && !empty($param['enddt_bao'])){
            $where[] = ['bid.enddt_bao','between', explode(' - ', $param['enddt_bao'])];
        }
        if(isset($param['enddt_tou']) && !empty($param['enddt_tou'])){
            $where[] = ['bid.enddt_tou','between', explode(' - ', $param['enddt_tou'])];
        }
        if(isset($param['kaidt']) && !empty($param['kaidt'])){
            $where[] = ['bid.kaidt','between', explode(' - ', $param['kaidt'])];
        }
        if(isset($param['touer_name']) && !empty($param['touer_name'])){
            $where[] = ['tou.realname','like', '%'.$param['touer_name'].'%'];
        }
        if(isset($param['touunit']) && !empty($param['touunit'])){
            $where[] = ['bid.touunit','=', $param['touunit']];
        }
        if(isset($param['toushuo']) && !empty($param['toushuo'])){
            $where[] = ['bid.toushuo','like', '%'.$param['toushuo'].'%'];
        }
        if(isset($param['lianunit']) && !empty($param['lianunit'])){
            $where[] = ['bid.lianunit','like', '%'.$param['lianunit'].'%'];
        }
        if(isset($param['isfei']) && !empty($param['isfei'])){
            $where[] = ['bid.isfei','=', $param['isfei']];
        }
        if(isset($param['feishuo']) && !empty($param['feishuo'])){
            $where[] = ['bid.feishuo','like', '%'.$param['feishuo'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['bid.state','=', $param['state']];
        }



        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['bid.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['touunit'] = $data['chanid'];
        $data['is_touunit'] = isset($param['touunit']) ? $param['touunit'] : '';

        $data['bidtype'] = (new OptService())->get_base_info('engin_engintype');
        $data['is_bidtype'] = isset($param['bidtype']) ? $param['bidtype'] : '';

        $data['zhaotype'] = (new OptService())->get_base_info('bid_bidding_type');
        $data['is_zhaotype'] = isset($param['zhaotype']) ? $param['zhaotype'] : '';

        $data['state'] = (new BidService())->get_bid_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['data'] = BidModel::alias('bid')
            ->field('bid.*,tou.realname as touer_name')
            ->leftJoin('user tou','tou.id=bid.touer')
            ->where($where)
            ->where($whereMy)
            ->order('bid.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['bidtype_name'] = (new OptService())->get_base_showname('engin_engintype', $item['bidtype']);
                $item['zhaotype_name'] = (new OptService())->get_base_showname('bid_bidding_type', $item['zhaotype']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['state_name'] = (new BidService())->get_bid_state_name_tag($item);
                $item['touunit_name'] = DeptModel::where(['id'=>$item['touunit']])->value('name');
                $item['bianer_name'] = UserModel::get_realname_by_ids($item['bianer']);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }







    /************************************ 渠道客户管理 ************************************/

    public function list_bid_customer(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];


        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['bid.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['address']) && !empty($param['address'])){
            $where[] = ['bid.address','like','%'.$param['address'].'%'];
        }
        if(isset($param['source']) && !empty($param['source'])){
            $where[] = ['bid.source','=',$param['source']];
        }
        if(isset($param['type']) && !empty($param['type'])){
            $where[] = ['bid.type','=',$param['type']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['bid.state','=',$param['state']];
        }
        if(isset($param['contacts']) && !empty($param['contacts'])){
            $where[] = ['bid.contacts','like', '%'.$param['contacts'].'%'];
        }
        if(isset($param['contacts_tel']) && !empty($param['contacts_tel'])){
            $where[] = ['bid.contacts_tel','like', '%'.$param['contacts_tel'].'%'];
        }
        if(isset($param['contacts_addr']) && !empty($param['contacts_addr'])){
            $where[] = ['bid.contacts_addr','like', '%'.$param['contacts_addr'].'%'];
        }
        if(isset($param['belong']) && !empty($param['belong'])){
            $where[] = ['u.realname','like', '%'.$param['belong'].'%'];
        }
        if(isset($param['created_user']) && !empty($param['created_user'])){
            $where[] = ['uc.realname','like', '%'.$param['created_user'].'%'];
        }




        $data['data'] = BidCustomerModel::alias('bid')->field('bid.*,u.realname as belong_name,uc.realname as created_user')
            ->leftJoin('user u','u.id = bid.belong')
            ->leftJoin('user uc','uc.id = bid.created_user')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid_customer', 'bid'))
            ->order('bid.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['source'] = (new OptService())->get_base_showname('bid_customer_source',$item['source']);
                $item['type'] = (new OptService())->get_base_showname('bid_customer_type',$item['type']);
                $item['state'] = (new OptService())->get_base_showname('bid_customer_state',$item['state']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('bid_customer');
        $data['source'] = (new OptService())->get_base_info('bid_customer_source');
        $data['type'] = (new OptService())->get_base_info('bid_customer_type');
        $data['state'] = (new OptService())->get_base_info('bid_customer_state');
        $data['address'] = (new OptService())->get_city_cader();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function add_bid_customer(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $result = BidCustomerModel::create($param);
                (new OptService())->relation_file($files,'bid_customer',$result->id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'bid_customer', $result->id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'bid_customer', $result->id, '客户');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['source'] = (new OptService())->get_base_info('bid_customer_source');
            $data['type'] = (new OptService())->get_base_info('bid_customer_type');
            $data['state'] = (new OptService())->get_base_info('bid_customer_state');

            $data['address'] = (new OptService())->get_city_cader();
            $data['belong_info'] = (new UserService())->get_users_part_arr($this->userinfo['id'], 'user');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }

    public function edit_bid_customer(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                BidCustomerModel::update($param);
                (new OptService())->relation_file($files,'bid_customer',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'bid_customer', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'bid_customer', $param['id'], '客户');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $result = BidCustomerModel::where(['id'=>$id,'deleted_user'=>0,'comid'=>$this->userinfo['id']])->find();
            if(!$result){
                return outputJson(4000,lang('code_4000'));
            }
            $data['data'] = $result;

            $data['source'] = (new OptService())->get_base_info('bid_customer_source');
            $data['type'] = (new OptService())->get_base_info('bid_customer_type');
            $data['state'] = (new OptService())->get_base_info('bid_customer_state');
            $data['address'] = (new OptService())->get_city_cader();
            $data['belong_info'] = (new UserService())->get_users_part_arr($result['belong'], 'user');
            $data['files'] = (new OptService())->get_file('bid_customer', $result['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function show_bid_customer(Request $request){
        $id = $request->post('id', 0, 'int');
        $data = BidCustomerModel::where(['id'=>$id,'deleted_user'=>0,'comid'=>$this->userinfo['comid']])->find();
        if(!$data){
            return outputJson(4000, lang('code_4000'));
        }
        $data['source'] = (new OptService())->get_base_showname('bid_customer_source', $data['source']);
        $data['type'] = (new OptService())->get_base_showname('bid_customer_type', $data['type']);
        $data['state'] = (new OptService())->get_base_showname('bid_customer_state', $data['state']);

        $data['belong_name'] = UserModel::get_realname_by_pk($data['belong']);
        $data['files'] = (new OptService())->get_file('bid_customer', $data['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function del_bid_customer(Request $request){
        $param = $request->post();
        $ids = array_filter(explode(',',$param['id']));
        if(count($ids) == 0){
            return outputJson(4000,'请选择需要数据');
        }

        Db::startTrans();
        try {
            foreach ($ids as $id){
                $data = BidCustomerModel::where(['id'=>$id,'comid'=>$this->userinfo['comid']])->find();
                if(!$data){
                    continue;
                }
                BidCustomerModel::where(['id' => $data['id']])->update([
                    'deleted_user' => $this->userinfo['id'],
                    'deleted_at' => date('Y-m-d H:i:s')
                ]);
                (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'bid', $data['id'], '客户');
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }


    /************************************ 标书 ************************************/
    public function list_bid_book(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['book.deleted_user','=','0'];
        $where[] = ['book.comid','=',$this->userinfo['comid']];
        $where[] = ['book.isturn','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['book.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['bid_name']) && !empty($param['bid_name'])){
            $where[] = ['b.name','like', '%'.$param['bid_name'].'%'];
        }
        if(isset($param['maker']) && !empty($param['maker'])){
            $where[] = ['u.realname','like', '%'.$param['maker'].'%'];
        }

        $data['data'] = BidBookModel::alias('book')->field('book.*,b.name as bid_name,b.business,b.enddt_tou,u.realname as maker_name')
            ->leftJoin('bid b','b.id = book.bidid')
            ->leftJoin('user u','u.id = book.maker')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid_book', 'book'))
            ->order('book.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['bill_dt'] = '';
                $billinfo = FlowBillModel::where('id',$item['billid'])->find();
                if($billinfo){
                    $item['bill_dt'] = $billinfo['updated_at'];
                }

                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['created_user'] = UserModel::get_realname_by_pk($item['created_user']);

                $item['business'] = $item['business'] == 1 ? '自营' : '联营';
                return $item;
            })->toArray();

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['butauth'] = (new MenuService())->get_butauth('bid_book');
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function show_bid_book(Request $request){
        $id = $request->post('id', 0, 'int');
        $data = BidBookModel::where(['id'=>$id,'deleted_user'=>0,'comid'=>$this->userinfo['comid']])->find();
        if(!$data){
            return outputJson(4000, lang('code_4000'));
        }
        $bid = BidModel::where('id',$data['bidid'])->find();
        $data['bid_name'] = $bid['name'];
        $data['business'] = $bid['business'] == 1 ? '自营' : '联营';

        $data['enddt_tou'] = $bid['enddt_tou'];

        $data['maker_name'] = UserModel::get_realname_by_pk($data['maker']);
        $data['chan_name'] = DeptModel::get_name_by_pk($data['chanid']);

        $data['files'] = (new OptService())->get_file('bid_book', $data['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['billid']);

        return outputJson(2000,lang('code_2000'), $data);
    }

    // 标书制作一览
    public function count_bid_book(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['book.deleted_user','=','0'];
        $where[] = ['book.comid','=',$this->userinfo['comid']];
        $where[] = ['book.isturn','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['book.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['bid_name']) && !empty($param['bid_name'])){
            $where[] = ['b.name','like', '%'.$param['bid_name'].'%'];
        }
        if(isset($param['maker']) && !empty($param['maker'])){
            $where[] = ['u.realname','like', '%'.$param['maker'].'%'];
        }

        $makerIds = BidBookModel::where(['deleted_user'=>0,'comid'=>$this->userinfo['comid']])->column('maker');
        $where[] = ['u.id','in',$makerIds];


        $data['data'] = UserModel::alias('u')
            ->field('book.*,b.name as bid_name,b.business,b.enddt_tou,b.state,b.touunit,b.kaidt,u.realname as maker_name,b.isqi')
            ->leftJoin('bid_book book','u.id = book.maker')
            ->leftJoin('bid b','b.id = book.bidid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid_book_count', 'book'))
            ->order('book.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['bill_dt'] = '';
                $billinfo = FlowBillModel::where('id',$item['billid'])->find();
                if($billinfo){
                    $item['bill_dt'] = $billinfo['updated_at'];
                }

                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['created_user'] = UserModel::get_realname_by_pk($item['created_user']);

                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['state'] = (new BidService())->get_bid_state_name_tag($item);
                $touunit = DealcomModel::where('id','in',$item['touunit'])->column('name');
                $item['touunit'] = implode('，',$touunit);
                return $item;
            })->toArray();

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        return outputJson(2000,lang('code_2000'),$data);
    }



    /************************************ 授权委托书 ************************************/
    public function list_bid_grant_book(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['book.deleted_user','=','0'];
        $where[] = ['book.comid','=',$this->userinfo['comid']];
        $where[] = ['book.isturn','=',1];

        if(isset($param['bid_name']) && !empty($param['bid_name'])){
            $where[] = ['b.name','like', '%'.$param['bid_name'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['book.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['created_user']) && !empty($param['created_user'])){
            $where[] = ['u.realname','like', '%'.$param['created_user'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['book.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['book.dt','between',[$param['dt'].' 00:00:00',$param['dt'].' 23:59:59']];
        }




        $data['data'] = BidGrantBookModel::alias('book')->field('book.*,b.name as bid_name,b.number as bid_number,
            b.business,b.state,b.isqi,u.realname as created_user')
            ->leftJoin('bid b','b.id = book.bidid')
            ->leftJoin('user u','u.id = book.created_user')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid_grant_book', 'book'))
            ->order('book.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['bid_business'] = $item['business'] == 1 ? '自营' : '联营';
                $item['bid_state'] = (new BidService())->get_bid_state_name_tag($item);
                $item['name'] = str_replace('[]','，',$item['name']);
                $item['number'] = str_replace('[]','，',$item['number']);
                $item['dt'] = $item['startdt'].' - '.$item['enddt'];

                $item['bill_dt'] = '';
                $billinfo = FlowBillModel::where('id',$item['billid'])->find();
                if($billinfo){
                    $item['bill_dt'] = $billinfo['updated_at'];
                }
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                return $item;
            })->toArray();

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['butauth'] = (new MenuService())->get_butauth('bid_grant_book');
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function show_bid_grant_book(Request $request){
        $id = $request->post('id', 0, 'int');
        $data = BidGrantBookModel::where(['id'=>$id,'deleted_user'=>0,'comid'=>$this->userinfo['comid']])->find();
        if(!$data){
            return outputJson(4000, lang('code_4000'));
        }
        $bid = BidModel::where('id',$data['bidid'])->find();
        $data['bid_name'] = $bid['name'];
        $data['bid_number'] = $bid['number'];

        $data['name'] = str_replace('[]','，',$data['name']);
        $data['number'] = str_replace('[]','，',$data['number']);
        $data['dt'] = $data['startdt'].' - '.$data['enddt'];

        $data['files'] = (new OptService())->get_file('bid_grant_book', $data['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }




    /************************************ 投标保证金 ************************************/
    public function list_bid_bond(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['bid.chanid','=', $param['chanid']];
        }
        if(isset($param['bid_name']) && !empty($param['bid_name'])){
            $where[] = ['bid.name','like', '%'.$param['bid_name'].'%'];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['bid.business','eq',$param['business']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['bid.state','eq', $param['state']];
        }
        if(isset($param['bond_type']) && !empty($param['bond_type'])){
            $where[] = ['bid.bond_type','eq', $param['bond_type']];
        }




        $state = (new BidService())->get_bid_state();
        $data['data'] = BidModel::alias('bid')
            ->field('bid.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid_bond', 'bid'))
            ->order('bid.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';

                $item['state_name'] = (new BidService())->get_bid_state_name_tag($item);
                $item['state_name_wen'] = (new BidService())->get_bid_state_name_wen($item);
                // 收取金额（元）,缴纳金额（元）,退回金额（元）,返还金额(元)
                $item['shou_money'] = EnginBondShouModel::where(['deleted_user'=>0,'bondtype'=>1,'status'=>1,'bidid'=>$item['id']])->sum('money');
                $item['jiao_money'] = EnginBondJiaoModel::where(['deleted_user'=>0,'status'=>1,'bidid'=>$item['id']])->sum('money');
                $jiaoIds = EnginBondJiaoModel::where(['deleted_user'=>0,'status'=>1,'bidid'=>$item['id']])->column('id');
                $item['tui_money'] = AccountListModel::where(['type'=>8])->where('mid','in',$jiaoIds)->sum('money');
                $item['fan_money'] = EnginBondFanModel::alias('fan')
                    ->leftJoin('engin_bond_shou shou','shou.id = fan.shouid')
                    ->where(['shou.deleted_user'=>0,'shou.status'=>1,'shou.bidid'=>$item['id'],
                        'fan.deleted_user'=>0,'fan.status'=>1])
                    ->sum('fan.money');
                return $item;
            })->toArray();

        $data['state'] = $state;
        $data['bond_type'] = BidModel::get_bond_type();
        $data['butauth'] = (new MenuService())->get_butauth('bid_bond');
        $data['chanid'] = (new UserService())->get_chaninfo_all();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function edit_bid_bond(Request $request){
        if($request->isPost()){
            $param = $request->post();

            $files = array_filter($param['files']);
            unset($param['files']);

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            Db::startTrans();
            try {
                BidModel::update($param);
                (new OptService())->relation_file($files,'bid',$param['id']);
                (new OptService())->add_flow_log('编辑保证金', 'bid', $param['id']);
                (new OptService())->add_log_list('编辑保证金', 'bid', $param['id'], '保证金');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $result = BidModel::where(['id'=>$id,'deleted_user'=>0,'comid'=>$this->userinfo['comid']])->find();
            if(!$result){
                return outputJson(4000,lang('code_4000'));
            }
            $data['data'] = $result;
            $data['bond_type'] = BidModel::get_bond_type();
            $data['files'] = (new OptService())->get_file('bid', $result['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    /************************************ 投标任务-通用模板 ************************************/
    public function list_bid_task_template(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];

        $data['data'] = BidTaskTemplateModel::alias('bid')
            ->field('bid.*')
            ->where($where)
            ->select()
            ->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }


    /************************************ 总结 ************************************/
    public function list_bid_sum(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['sum.deleted_user','=','0'];
        $where[] = ['sum.comid','=',$this->userinfo['comid']];
        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.isturn','=',1];
        $where[] = ['bid.status','=',1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['bid.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['bid.state','=', $param['state']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['bid.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['bid.business','=', $param['business']];
        }
        if(isset($param['created_user_name']) && !empty($param['created_user_name'])){
            $where[] = ['user.realname','like', '%'.$param['created_user_name'].'%'];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new BidService())->get_bid_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['data'] = BidSumModel::alias('sum')
            ->join('bid bid','bid.id = sum.bidid')
            ->join('user user','user.id = sum.created_user')
            ->field('sum.*,bid.name as bid_name,bid.state,bid.number,bid.bidtype,bid.areas,bid.zhaoname,bid.zhaotel,bid.zhaotype,
            bid.touer,bid.kaidt,bid.tou_kaitype,bid.chanid,bid.business,bid.tou_fee,bid.status,bid.isqi,bid.touer')
            ->where($where)
            ->where((new MenuService())->get_miss_where('bid_sum', 'sum'))
            ->order('sum.id desc')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['state_name'] = (new BidService())->get_bid_state_name_tag($item);
                $item['state_name_wen'] = (new BidService())->get_bid_state_name_wen($item);
                $item['bidtype_name'] = (new OptService())->get_base_showname('engin_engintype', $item['bidtype']);
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['zhaotype_name'] = (new OptService())->get_base_showname('bid_bidding_type', $item['zhaotype']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['touer_name'] = UserModel::get_realname_by_pk($item['touer']);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('bid_sum');
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function add_bid_sum(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $check = $this->validate($param,'BidSum.add');
            if($check !== true){
                return outputJson(4000,$check);
            }
            // 所有的明细处理成数组形式
            $list = array_filter($param['list']);
            unset($param['list']);

            $files = array_filter($param['files']);
            unset($param['files']);

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            Db::startTrans();
            try {
                $result = BidSumModel::create($param);

                foreach ($list as $item){
                    $item['mid'] = $result->id;
                    BidSumListModel::create($item);
                }

                (new OptService())->relation_file($files,'bid_sum',$result->id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'bid_sum', $result->id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'bid_sum', $result->id, '投标总结');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }
    }

    public function edit_bid_sum(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $check = $this->validate($param,'BidSum.edit');
            if($check !== true){
                return outputJson(4000,$check);
            }

            // 所有的明细处理成数组形式
            $list = array_filter($param['list']);
            unset($param['list']);

            $files = array_filter($param['files']);
            unset($param['files']);

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');

            Db::startTrans();
            try {
                BidSumModel::update($param);

                BidSumListModel::where('mid',$param['id'])->delete();
                foreach ($list as $item){
                    $item['mid'] = $param['id'];
                    BidSumListModel::create($item);
                }

                (new OptService())->relation_file($files,'bid_sum',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'bid_sum', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'bid_sum', $param['id'], '投标总结');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $result = BidSumModel::where(['id'=>$id,'deleted_user'=>0,'comid'=>$this->userinfo['comid']])->find();
            if(!$result){
                return outputJson(4000,lang('code_4000'));
            }
            $bidinfo = BidModel::where('id',$result['bidid'])->find();
            $bidinfo['state_name'] = (new BidService())->get_bid_state_name_tag($bidinfo);
            $bidinfo['state_name_wen'] = (new BidService())->get_bid_state_name_wen($bidinfo);
            $bidinfo['bidtype_name'] = (new OptService())->get_base_showname('engin_engintype', $bidinfo['bidtype']);
            $bidinfo['chanid_name'] = DeptModel::get_name_by_pk($bidinfo['chanid']);
            $bidinfo['zhaotype_name'] = (new OptService())->get_base_showname('bid_bidding_type', $bidinfo['zhaotype']);
            $bidinfo['business_name'] = $bidinfo['business'] == 1 ? '自营' : '联营';
            $bidinfo['touer_name'] = UserModel::get_realname_by_pk($bidinfo['touer']);
            $data['bidinfo'] = $bidinfo;
            $data['data'] = $result;

            $data['list'] = BidSumListModel::where('mid',$result['id'])->select()->toArray();
            $data['files'] = (new OptService())->get_file('bid_sum', $id);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }

    public function show_bid_sum(Request $request){
        $id = $request->post('id', 0, 'int');
        $data = BidSumModel::where(['id'=>$id])->find();
        if(!$data){
            return outputJson(4000, lang('code_4000'));
        }

        $bidinfo = BidModel::where('id',$data['bidid'])->find();
        $bidinfo['state_name'] = (new BidService())->get_bid_state_name_tag($bidinfo);
        $bidinfo['state_name_wen'] = (new BidService())->get_bid_state_name_wen($bidinfo);
        $bidinfo['bidtype_name'] = (new OptService())->get_base_showname('engin_engintype', $bidinfo['bidtype']);
        $bidinfo['chanid_name'] = DeptModel::get_name_by_pk($bidinfo['chanid']);
        $bidinfo['zhaotype_name'] = (new OptService())->get_base_showname('bid_bidding_type', $bidinfo['zhaotype']);
        $bidinfo['business_name'] = $bidinfo['business'] == 1 ? '自营' : '联营';
        $bidinfo['touer_name'] = UserModel::get_realname_by_pk($bidinfo['touer']);
        $data['bidinfo'] = $bidinfo;

        $data['list'] = BidSumListModel::where('mid',$data['id'])->select()->toArray();
        $data['files'] = (new OptService())->get_file('bid_sum', $data['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function del_bid_sum(Request $request){
        $param = $request->post();
        $ids = array_filter(explode(',',$param['id']));
        if(count($ids) == 0){
            return outputJson(4000,'请选择需要删除的投标总结');
        }

        Db::startTrans();
        try {
            foreach ($ids as $id){
                $data = BidSumModel::where(['id'=>$id,'comid'=>$this->userinfo['comid']])->find();
                if(!$data){
                    continue;
                }
                BidSumModel::where(['id' => $data['id']])->update([
                    'deleted_user' => $this->userinfo['id'],
                    'deleted_at' => date('Y-m-d H:i:s')
                ]);
                (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'bid_sum', $data['id'], '投标总结');
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }






    public function bid_count_yeartou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];
        $where[] = ['bid.isturn','=',1];
        $where[] = ['bid.status','=',1];

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['bid.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $min_enddt_tou = BidModel::alias('bid')->where($where)
            ->where("bid.enddt_tou != '' and bid.enddt_tou is not null and bid.enddt_tou != '0000-00-00'")
            ->order('bid.enddt_tou ASC')->value('bid.enddt_tou');
        $max_enddt_tou = BidModel::alias('bid')->where($where)
            ->where("bid.enddt_tou != '' and bid.enddt_tou is not null and bid.enddt_tou != '0000-00-00'")
            ->order('bid.enddt_tou DESC')->value('bid.enddt_tou');
        if(!$min_enddt_tou){
            $data['data'] = [];
            return outputJson(2000,lang('code_2000'),$data);
        }
        $startYear = date('Y', strtotime($max_enddt_tou));
        $endYear = date('Y', strtotime($min_enddt_tou));

        $list = [];
        for ($i=(int)$startYear; $i>=$endYear; $i--){
            $bet1 = $i.'-01-01 00:00:00';
            $bet2 = $i.'-12-31 23:59:59';
            $zainum = BidModel::alias('bid')->where($where)->where(['state'=>1])->where('bid.enddt_tou','between',[$bet1, $bet2])->count();
            $winnum = BidModel::alias('bid')->where($where)->where(['state'=>3])->where('bid.enddt_tou','between',[$bet1, $bet2])->count();
            $luonum = BidModel::alias('bid')->where($where)->where(['state'=>6])->where('bid.enddt_tou','between',[$bet1, $bet2])->count();
            $list[] = [
                'year' => $i,
                'allnum' => $zainum+$winnum+$luonum,
                'zainum' => $zainum,
                'winnum' => $winnum,
                'luonum' => $luonum,
            ];
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function bid_count_yearkai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];
        $where[] = ['bid.isturn','=',1];
        $where[] = ['bid.status','=',1];

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['bid.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $min_kaidt = BidModel::alias('bid')->where($where)
            ->where("bid.kaidt != '' and bid.kaidt is not null and bid.kaidt != '0000-00-00'")
            ->order('bid.kaidt ASC')->value('bid.kaidt');
        $max_kaidt = BidModel::alias('bid')->where($where)
            ->where("bid.kaidt != '' and bid.kaidt is not null and bid.kaidt != '0000-00-00'")
            ->order('bid.kaidt DESC')->value('bid.kaidt');
        if(!$min_kaidt){
            $data['data'] = [];
            return outputJson(2000,lang('code_2000'),$data);
        }
        $startYear = date('Y', strtotime($max_kaidt));
        $endYear = date('Y', strtotime($min_kaidt));

        $list = [];
        for ($i=(int)$startYear; $i>=$endYear; $i--){
            $bet1 = $i.'-01-01 00:00:00';
            $bet2 = $i.'-12-31 23:59:59';
            $allnum = BidModel::alias('bid')->where($where)->where('state','in',[3,6])->where('bid.kaidt','between',[$bet1, $bet2])->count();
            $winnum = BidModel::alias('bid')->where($where)->where(['state'=>3])->where('bid.kaidt','between',[$bet1, $bet2])->count();
            $winfee = BidModel::alias('bid')->where($where)->where(['state'=>3])->where('bid.kaidt','between',[$bet1, $bet2])->sum('kai_fee');
            $winrate = 0;
            if($allnum != 0){
                $winrate = round($winnum/$allnum*100, 2);
            }

            $list[] = [
                'year' => $i,
                'allnum' => $allnum,
                'winnum' => $winnum,
                'winfee' => $winfee,
                'winrate' => $winrate,
            ];
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function bid_count_win(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bid.deleted_user','=','0'];
        $where[] = ['bid.comid','=',$this->userinfo['comid']];
        $where[] = ['bid.isturn','=',1];
        $where[] = ['bid.status','=',1];

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['bid.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $min_kaidt = BidModel::alias('bid')->where($where)
            ->where("bid.kaidt != '' and bid.kaidt is not null and bid.kaidt != '0000-00-00'")
            ->order('bid.kaidt ASC')->value('bid.kaidt');
        $max_kaidt = BidModel::alias('bid')->where($where)
            ->where("bid.kaidt != '' and bid.kaidt is not null and bid.kaidt != '0000-00-00'")
            ->order('bid.kaidt DESC')->value('bid.kaidt');
        if(!$min_kaidt){
            $data['data'] = [];
            return outputJson(2000,lang('code_2000'),$data);
        }
        $startYear = date('Y', strtotime($max_kaidt));
        $endYear = date('Y', strtotime($min_kaidt));

        $list = [];
        for ($i=(int)$startYear; $i>=$endYear; $i--){
            $bet1 = $i.'-01-01 00:00:00';
            $bet2 = $i.'-12-31 23:59:59';
            $zi_num = BidModel::alias('bid')->where($where)->where(['state'=>3,'business'=>1])->where('bid.kaidt','between',[$bet1, $bet2])->count();
            $zi_fee = BidModel::alias('bid')->where($where)->where(['state'=>3,'business'=>1])->where('bid.kaidt','between',[$bet1, $bet2])->sum('kai_fee');
            $lian_num = BidModel::alias('bid')->where($where)->where(['state'=>3,'business'=>2])->where('bid.kaidt','between',[$bet1, $bet2])->count();
            $lian_fee = BidModel::alias('bid')->where($where)->where(['state'=>3,'business'=>2])->where('bid.kaidt','between',[$bet1, $bet2])->sum('kai_fee');

            $list[] = [
                'year' => $i,
                'zi_num' => $zi_num,
                'zi_fee' => $zi_fee,
                'lian_num' => $lian_num,
                'lian_fee' => $lian_fee,
            ];
        }

        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }



}

















